<?php

namespace ApiBundle\Controller\User;


use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class FileController extends BaseController
{
    /*
     * 分类
     */
    public function getFileCategoryAction()
    {

        $conn = $this->get("database_connection");
        if(!$this->isLogged($conn)){
            return $this->response;
        }
        $district_id = $conn->fetchAssoc("SELECT district_id FROM user WHERE id = '". $this->getUserId() ."'");

        $data = $conn->fetchAll("SELECT * FROM file_category WHERE district_id = '". $district_id['district_id'] ."'");
        return $this->jsonResponse(0,"操作成功",$data);
    }

    /*
     * 得到文件列表
     */
    public function getFileMessageAction(Request $request)
    {
        $conn = $this->get("database_connection");

        if(!$this->isLogged($conn)){
            return $this->response;
        }
        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $sql = " SELECT * FROM file WHERE file_category_id = '". $id ."' AND is_public = 1 AND id IN (SELECT file_id FROM user_file WHERE user_id = '". $this->getUserId() ."')";

        // TODO 搜索
        $search = $request->get("search_keyword");
        if(!empty($search)){

            $sql .= " AND ( title LIKE '%". $search ."%' OR keyword LIKE '%". $search ."%' ) ";
        }

        $sql .= " ORDER BY created_at DESC ";

        $data = $conn->fetchAll($sql);



        return $this->jsonResponse(0,"操作成功",array("info" => $data));
    }

    /*
     * 得到文件列表详情
     */
    public function getFileDetailAction(Request $request)
    {
        $conn = $this->get("database_connection");

        if(!$this->isLogged($conn)){
            return $this->response;
        }
        $id = $request->get("id");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $data = $conn->fetchAssoc("SELECT f.*, a.path AS attachment_path, a.origin_name AS attachment_origin_name, a.size AS attachment_size FROM file f LEFT JOIN attachment a ON f.attachment_id = a.id WHERE f.id = '". $id ."'");
        return $this->jsonResponse(0,"操作成功",array("info" => $data));
    }


    /*
     * 获取下载记录
     */
    public function downloadRecordAction()
    {
        $conn = $this->get("database_connection");
        if(!$this->isLogged($conn)){
            return $this->response;
        }
        $data = $conn->fetchAll("SELECT d.id, d.attachment_id, d.user_id, d.created_at, a.path, a.origin_name, a.size FROM downloadRecord d LEFT JOIN attachment a ON d.attachment_id = a.id WHERE d.user_id = '". $this->getUserId() ."' ORDER BY d.created_at DESC");
        return $this->jsonResponse(0,"操作成功",$data);
    }

    /*
     * 添加下载记录
     */
    public function addDownloadAction(Request $request)
    {
        $conn = $this->get("database_connection");
        if(!$this->isLogged($conn)){
            return $this->response;
        }

        $attachment_id = $request->get("attachment_id");
        if(empty($attachment_id)){
            return $this->jsonResponse(1,"attachment_id参数错误");
        }

        $data = array(
            "attachment_id" => $attachment_id,
            "user_id" => $this->getUserId(),
            "created_at" => date('Y-m-d H:i:s')
        );

        $conn->insert("downloadRecord",$data);

        return $this->jsonResponse(0,"操作成功");


    }



}
